const util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pid: 0,
    mid: 0,
    cfgData: [],
    users: wx.getStorageSync('USERS')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      pid: options.pid,
      mid: options.mid
    });
    if (!that.data.users.id) {
      that.setData({
        users: wx.getStorageSync('USERS')
      });
    }
    util.wxPost({
      url: "/rest/editConfig", params: { "uid": that.data.users.id, "pid": that.data.pid, "mid": that.data.mid },
      success: function (data) {
        that.setData({
          cfgData: data.data.data
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  formSubmit: function (e) {
    var that = this;
    var gridsAmount = e.detail.value.gridsAmount;
    var gridsEchelon = e.detail.value.gridsEchelon;
    var buyStep = parseFloat(e.detail.value.buyStep);
    var sellStep = parseFloat(e.detail.value.sellStep);
    var gridsTax = parseFloat(e.detail.value.gridsTax);
    var agentTax = e.detail.value.agentTax;
    var vali = [];
    if (!gridsAmount || gridsAmount <= 0) {
      vali.push("gridsAmount");
      wx.showToast({
        icon: "none",
        title: "网格资金不能空或者小于等于0!"
      });
    }
    if (!gridsEchelon) {
      vali.push("gridsEchelon");
      wx.showToast({
        icon: "none",
        title: "网格深度不能为空!"
      });
    }
    if (that.data.users.vip == "0" && gridsEchelon > 3){
      vali.push("gridsEchelonCount");
      wx.showToast({
        icon: "none",
        title: "非会员体验期网格深度最多只能3格!"
      });
    }
    if (!buyStep || buyStep <= 0) {
      vali.push("buyStep");
      wx.showToast({
        icon: "none",
        title: "买进比例不能为空或者小于等于0!"
      });
    }
    if (!sellStep || sellStep <= 0) {
      vali.push("sellStep");
      wx.showToast({
        icon: "none",
        title: "卖出比例不能为空或者小于等于0!"
      });
    }
    if (!gridsTax) {
      vali.push("gridsTax");
      wx.showToast({
        icon: "none",
        title: "交易税率不能为空!"
      });
    }
    if (vali.length == 0) {
      var params = {
        uid: that.data.users.id,
        mid: that.data.mid,
        pid: that.data.pid,
        gridsAmount: gridsAmount,
        gridsEchelon: gridsEchelon,
        buyStep: buyStep,
        sellStep: sellStep,
        gridsTax: gridsTax,
        agentTax: agentTax
      }
      util.wxPost({
        url: "/rest/updateConfig", params: params,
        success: function (data) {
          if (data.data.status == "000") {
            wx.showToast({
              title: "保成成功",
              icon: "success",
              duration: 1500,
              complete: function () {
                wx.navigateTo({
                  url: '/pages/common/gridsConfig?mid=' + params.mid + '&pid=' + params.pid,
                });
              }
            });
          }
        }
      });
    }
  }
})